// components/toast/toast.js
Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    title: {
      type: String,
      value: ''
    },
    icon: {
      type: String,
      value: 'none'  // success, error, loading, none
    },
    duration: {
      type: Number,
      value: 2000
    },
    mask: {
      type: Boolean,
      value: false
    }
  },
  
  data: {
    animationData: {}
  },
  
  observers: {
    'show': function(show) {
      if (show) {
        this.showToast();
      }
    }
  },
  
  methods: {
    showToast() {
      // 显示动画
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'ease',
      });
      
      animation.opacity(1).step();
      
      this.setData({
        animationData: animation.export()
      });
      
      // 设置定时器，自动隐藏
      if (this.data.duration > 0) {
        setTimeout(() => {
          this.hideToast();
        }, this.data.duration);
      }
    },
    
    hideToast() {
      // 隐藏动画
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'ease',
      });
      
      animation.opacity(0).step();
      
      this.setData({
        animationData: animation.export()
      });
      
      setTimeout(() => {
        this.setData({
          show: false
        });
        // 触发关闭事件
        this.triggerEvent('close');
      }, 200);
    }
  }
}) 